package com.weaver.qfengx.action;

import com.google.common.collect.Maps;
import com.weaver.qfengx.*;
import org.apache.commons.compress.utils.Lists;
import weaver.interfaces.workflow.action.OperatorAction;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public abstract class BaseOperatorAction implements OperatorAction {

    protected LogUtils log = new LogUtils(this.getClass());

    private String actionName = "封装后的流程节点OperatorAction基础类";

    public void setActionName(String name) {
        this.actionName = name;
    }

    protected void setLog(Class clazz) {
        this.log = new LogUtils(clazz);
    }

    protected String workflowid = "";
    protected String requestid = "";
    protected String creatorname = "";
    protected String creatorid = "";
    protected String formid = "";
    protected String maintable = "";
    protected String requestname = "";
    protected Map<String, String> mainTableMap = Maps.newHashMap();

    protected boolean isOk = true;
    protected String message = "";

    public abstract List<String> execCode(RequestInfo requestInfo) throws Exception;

    @Override
    public List<String> execute(RequestInfo requestInfo) {
        try {
            log.writeLog("=========开始执行:" + actionName +" OperatorAction==============> " + DateUtils.datetime());
            workflowid = requestInfo.getWorkflowid();
            requestid = requestInfo.getRequestid();
            creatorname = requestInfo.getCreatorid();
            creatorid = WorkflowUtils.creatorId(requestid);
            formid = DaoUtils.querySingleVal("select formid from workflow_base where id = ?", workflowid);
            maintable = "formtable_main_" + -NumberUtils.parseInt(formid);
            requestname = DaoUtils.querySingleVal("select requestname from workflow_requestbase where requestid = ?", requestid);
            log.writeLog("=======> 流程基本信息 <======");
            log.writeLog("requestname => " + requestname, ", workflowid => " + workflowid + ", creatorid => " + creatorid + ", creatorname => " + creatorname);
            log.writeLog("formid => " + formid + ", maintable => " + maintable);
            // 主表信息
            MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
            for (Property property : mainTableInfo.getProperty()) {
                log.writeLog("propertyname => " + property.getName() + ", propertyvalue => " + property.getValue());
                mainTableMap.put(property.getName(), property.getValue());
            }

            long startExecCode = System.currentTimeMillis();
            log.writeLog("开始执行业务逻辑代码 => " + DateUtils.datetime());
//            业务逻辑代码开始
            List<String> execRes = execCode(requestInfo);
//            业务逻辑代码结束
            log.writeLog("结束执行业务逻辑代码 => cost: " + (System.currentTimeMillis() - startExecCode) + ", " + DateUtils.datetime());
            log.writeLog("=========执行结束" + actionName + " OperatorAction==============> " + DateUtils.datetime());
            log.writeLog("审批人员列表 => " + Arrays.toString(execRes.toArray()));
            return execRes;

        } catch (Exception e) {
            log.writeLog("=========执行异常：" + actionName + " OperatorAction " + DateUtils.datetime() + " => " + e.getMessage());
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent(actionName + " OperatorAction执行异常: " + e.getMessage());
            requestInfo.getRequestManager().setMessageid(requestid);
            throw new RuntimeException(e.getMessage());
//            return Lists.newArrayList();
        }
    }

}
